package com.scott;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
@ResponseBody
// @RestController
public class HelloController {
    @RequestMapping("/hello")
    public Map<String, String> hello() {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 100; i++) {
            map.put(String.valueOf(i), UUID.randomUUID().toString());
        }
        return map;
    }

    private List<byte[]> list = new ArrayList<>();
    private AtomicInteger integer = new AtomicInteger();

    @RequestMapping("/hello2")
    public Map<String, Object> hello2() {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put(String.valueOf(i), new byte[1024]);

            list.add(new byte[128]);
        }

        if (integer.get() == 50) {
            list = new ArrayList<>();
        }
        try {
            Thread.sleep(100);
            integer.getAndIncrement();
        } catch (InterruptedException e) {
        }

        return map;
    }

    private List<byte[]> list2 = new ArrayList<>();
    private AtomicInteger integer2 = new AtomicInteger(0);

    private List<byte[]> list3 = null;
    private List<byte[]> list4 = null;

    @RequestMapping("/hello3")
    public Map<String, Object> hello3() {
        Map<String, Object> map = new HashMap<>();

        for (int i = 0; i < 10; i++) {
            list2.add(new byte[1024]);
        }

        if (integer2.getAndIncrement() == 1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(50000);
                        } catch (InterruptedException e) {
                        }

                        list3.add(new byte[128]);

                        list4 = new ArrayList<>();
                        list4.add(new byte[1024]);

                        list2.clear();
                    }
                }
            });
        }

        return map;
    }

    @RequestMapping("/hello4")
    public Map<String, Object> hello4() {

        list = new ArrayList<>();
        list2 = new ArrayList<>();
        list3 = new ArrayList<>();
        list4 = new ArrayList<>();

        return new HashMap<>();
    }

}